package com.ruoyi.meeting.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.request.PageRequest;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresLogin;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.meeting.domain.entity.Meeting;
import com.ruoyi.meeting.service.IMeetingService;
import com.ruoyi.meeting.util.MapUtil;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import static com.ruoyi.common.core.web.domain.AjaxResult.success;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xyf
 * @since 2024-12-16
 */
@RestController
@RequestMapping("/meeting")
public class MeetingController {
    
    @Autowired
    private IMeetingService meetingService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @RequiresLogin
    @GetMapping("/delayMessageTest")
    public void delayMessageTest(){
        // 1.创建消息
        String message = "hello, delayed message";
        // 2.发送消息，利用消息后置处理器添加消息头
        rabbitTemplate.convertAndSend("delay.direct", "delay", message, new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                // 添加延迟消息属性
                message.getMessageProperties().setDelay(5000);
                return message;
            }
        });
    }


    @RequiresLogin
    @GetMapping("/getNavigationPath")
    public R<Object> getNavigationPath(@RequestParam("startLng") Double startLng,@RequestParam("startLat") Double startLat,
                                       @RequestParam("endLng") Double endLng,@RequestParam("endLat") Double endLat,
                                       @RequestParam("type") Integer type){
        if(startLat == null || startLng == null || endLat == null || endLng == null || type==null){
            throw new RuntimeException("请求参数有误");
        }
        Object navigationPath = MapUtil.getNavigationPath(startLng, startLat, endLng, endLat, type);
        return R.ok(navigationPath);
    }


    /**
     * 查询会议列表
     */
    @RequiresPermissions("meeting:meeting:list")
    @GetMapping("/list")
    public TableDataInfo list(PageRequest pageRequest) {
        Page<Meeting> page = meetingService.page(new Page<>(pageRequest.getPageNum(), pageRequest.getPageSize()));
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setCode(200);
        tableDataInfo.setMsg("查询成功");
        tableDataInfo.setRows(page.getRecords());
        tableDataInfo.setTotal(page.getTotal());
        return tableDataInfo;
    }

    /**
     * 导出会议列表
     */
    @RequiresPermissions("meeting:meeting:export")
    @Log(title = "会议", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PageRequest pageRequest)
    {
        Page<Meeting> page = meetingService.page(new Page<>(pageRequest.getPageNum(), pageRequest.getPageSize()));
        List<Meeting> list = page.getRecords();
        ExcelUtil<Meeting> util = new ExcelUtil<Meeting>(Meeting.class);
        util.exportExcel(response, list, "会议数据");
    }

    /**
     * 获取会议详细信息
     */
    @RequiresPermissions("meeting:meeting:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        Meeting meeting = meetingService.getById(id);
        return success(meeting);
    }

    /**
     * 新增会议
     */
    @RequiresPermissions("meeting:meeting:add")
    @Log(title = "会议", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Meeting meeting) {
        meeting.setCreateTime(new Date());
        boolean isSuccess = meetingService.save(meeting);
        return isSuccess ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 修改会议
     */
    @RequiresPermissions("meeting:meeting:edit")
    @Log(title = "会议", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Meeting meeting) {
        boolean isSuccess = meetingService.updateById(meeting);
        return isSuccess ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 删除会议
     */
    @RequiresPermissions("meeting:meeting:remove")
    @Log(title = "会议", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        List<Long> idList = Arrays.asList(ids);
        boolean isSuccess = meetingService.removeBatchByIds(idList);
        return isSuccess ? AjaxResult.success() : AjaxResult.error();
    }

}
